<?php
/*
Plugin Name: 腾讯云备份
Plugin URI: https://www.beizigen.com/post/wordpress-tencent-cos-real-time-backup-plug-in-backup-cos-simple/
Description: 将网站附件、数据库实时备份到腾讯COS云存储。
Version: 1.2.0
Author: 背字根
Author URI: https://www.beizigen.com
*/
require_once('functions.php');

function backup_cos_control()
{
	$message = '';
	if (isset($_POST['action']) && $_POST['action'] == '保存设置') {
		$updatas = array(
			'cycle' => $_POST['cycle'],
			'region' => $_POST['region'],
			'APP_ID' => trim($_POST['APP_ID']),
			'SecretID' => trim($_POST['SecretID']),
			'SecretKey' => trim($_POST['SecretKey']),
			'Bucket_Name' => trim($_POST['Bucket_Name']),
		);
		$updatas = serialize($updatas);
		update_option('backups_cos', $updatas);
		if (wp_next_scheduled('backups_cos_cron')) backup_cos_delete_cron();
		//创建定时任务
		if ($_POST['cycle'] != 'close') {
			wp_schedule_event(time(), $_POST['cycle'], 'backups_cos_cron');
		}
		$message = '设置已保存！';
	}
	if (isset($_POST['action']) && $_POST['action'] == '备份数据库') {
		$res = backup_cos_start();
		if (!$res) {
			$message = '备份成功，赶紧<a href="https://console.cloud.tencent.com/cos5/bucket" target="_blank">登录腾讯云存储</a>查看一下吧！';
		} else {
			$message = '备份失败，错误信息：' . $res;
		}
	}
	if ($message) {
		echo '<div class="notice is-dismissible updated" style="padding: 1em;">' . $message . '</div>';
	}
	$options = get_option('backups_cos');
	$options = unserialize($options);
?>

	<div class="wrap">
		<h2>腾讯云备份</h2>
		<p>上传附件实时备份到腾讯云存储，支持定时备份数据库。</p>
		<form method="post" action="">
			<table class="form-table">
				<tbody>
					<tr>
						<th scope="row"><label for="cycle">定时备份</label></th>
						<td>
							<select name="cycle" id="cycle">
								<option value="daily" <?php if ($options['cycle'] == 'daily') echo ' selected'; ?>>每天</option>
								<option value="weekly" <?php if ($options['cycle'] == 'weekly') echo ' selected'; ?>>每周</option>
								<option value="monthly" <?php if ($options['cycle'] == 'monthly') echo ' selected'; ?>>每月</option>
								<option value="close" <?php if ($options['cycle'] == 'close') echo ' selected'; ?>>关闭定时</option>
							</select>
							<p class="description">定时备份仅针对数据库，附件是实时同步到云存储的</p>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="region">所属地域</label></th>
						<td>
							<select name="region" id="region">
								<option value="ap-beijing-1" <?php echo $options['region'] == 'ap-beijing-1' ? ' selected="selected"' : ''; ?>>北京一区（华北）</option>
								<option value="ap-beijing" <?php echo $options['region'] == 'ap-beijing' ? ' selected="selected"' : ''; ?>>北京</option>
								<option value="ap-shanghai" <?php echo $options['region'] == 'ap-shanghai' ? ' selected="selected"' : ''; ?>>上海（华东）</option>
								<option value="ap-guangzhou" <?php echo $options['region'] == 'ap-guangzhou' ? ' selected="selected"' : ''; ?>>广州（华南）</option>
								<option value="ap-chengdu" <?php echo $options['region'] == 'ap-chengdu' ? ' selected="selected"' : ''; ?>>成都（西南）</option>
								<option value="ap-chongqing" <?php echo $options['region'] == 'ap-chongqing' ? ' selected="selected"' : ''; ?>>重庆</option>
								<option value="ap-singapore" <?php echo $options['region'] == 'ap-singapore' ? ' selected="selected"' : ''; ?>>新加坡</option>
								<option value="ap-hongkong" <?php echo $options['region'] == 'ap-hongkong' ? ' selected="selected"' : ''; ?>>香港</option>
								<option value="na-toronto" <?php echo $options['region'] == 'na-toronto' ? ' selected="selected"' : ''; ?>>多伦多</option>
								<option value="eu-frankfurt" <?php echo $options['region'] == 'eu-frankfurt' ? ' selected="selected"' : ''; ?>>法兰克福</option>
								<option value="ap-mumbai" <?php echo $options['region'] == 'ap-mumbai' ? ' selected="selected"' : ''; ?>>孟买</option>
								<option value="ap-seoul" <?php echo $options['region'] == 'ap-seoul' ? ' selected="selected"' : ''; ?>>首尔</option>
								<option value="na-siliconvalley" <?php echo $options['region'] == 'na-siliconvalley' ? ' selected="selected"' : ''; ?>>硅谷</option>
								<option value="na-ashburn" <?php echo $options['region'] == 'na-ashburn' ? ' selected="selected"' : ''; ?>>弗吉尼亚</option>
								<option value="ap-bangkok" <?php echo $options['region'] == 'ap-bangkok' ? ' selected="selected"' : ''; ?>>曼谷</option>
								<option value="eu-moscow" <?php echo $options['region'] == 'eu-moscow' ? ' selected="selected"' : ''; ?>>莫斯科</option>
							</select>
							<p class="description">创建存储桶时选择的所属地域</p>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label for="APP_ID">APP ID</label>
						</th>
						<td>
							<input id="APP_ID" name="APP_ID" class="regular-text code" type="text" value="<?php echo $options['APP_ID']; ?>" />
							<p class="description">APP ID，<a href="https://console.cloud.tencent.com/cam/capi" target="_blank">登录云API密钥</a>获取</p>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label for="SecretID">SecretID</label>
						</th>
						<td>
							<input id="SecretID" name="SecretID" class="regular-text code" type="text" value="<?php echo $options['SecretID']; ?>" />
							<p class="description">SecretID，<a href="https://console.cloud.tencent.com/cam/capi" target="_blank">登录云API密钥</a>获取</p>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label for="SecretKey">SecretKey</label>
						</th>
						<td>
							<input id="SecretKey" name="SecretKey" class="regular-text code" type="text" value="<?php echo $options['SecretKey']; ?>" />
							<p class="description">SecretKey，<a href="https://console.cloud.tencent.com/cam/capi" target="_blank">登录云API密钥</a>获取</p>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label for="Bucket_Name">Bucket Name</label>
						</th>
						<td>
							<input id="Bucket_Name" name="Bucket_Name" class="regular-text code" type="text" value="<?php echo $options['Bucket_Name']; ?>" />
							<p class="description">存储桶名称，<a href="https://console.cloud.tencent.com/cos5/bucket" target="_blank">登录腾讯云控制台</a>获取</p>
						</td>
					</tr>
				</tbody>
			</table>
			<p><input class="button-primary" name="action" type="submit" value="保存设置" />&nbsp;&nbsp;<input class="button" type="submit" name="action" value="备份数据库" /></p>
		</form>
	</div>

<?php
}
//添加菜单
function backup_cos_menu()
{
	add_menu_page('腾讯云备份', '腾讯云备份', 'administrator', 'backups_cos', 'backup_cos_control', 'dashicons-cloud');
}
add_action('admin_menu', 'backup_cos_menu');

function backup_cos_settings_link($action_links, $plugin_file)
{
	if ($plugin_file == plugin_basename(__FILE__)) {
		$settings_link = '<a href="admin.php?page=backups_cos">设置</a>';
		array_unshift($action_links, $settings_link);
	}
	return $action_links;
}
add_filter('plugin_action_links', 'backup_cos_settings_link', 10, 2);
?>